package com.git.smp.utils;


import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.Namespace;
import org.dom4j.QName;


import com.git.smp.entity.SmpField;

public class XsdUtil {
	
	public static void main(String[] args) throws IOException {
		List<SmpField> listField = new ArrayList<>();
		SmpField smpField = new SmpField();
		smpField.setFieldCode("Test1");
		smpField.setInOut("01");
		smpField.setFieldType("01");
		smpField.setStruct("01");
		smpField.setFieldRequire("01");
		smpField.setNodeType("01");
		smpField.setParentType("");
		listField.add(smpField);
		
		SmpField smpField1 = new SmpField();
		smpField1.setFieldCode("List");
		smpField1.setInOut("01");
		smpField1.setFieldType("01");
		smpField1.setStruct("01");
		smpField1.setFieldRequire("02");
		smpField1.setNodeType("03");
		smpField1.setParentType("");
		listField.add(smpField1);
		
		
		SmpField smpField2 = new SmpField();
		smpField2.setFieldCode("Detail");
		smpField2.setInOut("01");
		smpField2.setFieldType("01");
		smpField2.setStruct("01");
		smpField2.setFieldRequire("02");
		smpField2.setNodeType("02");
		smpField2.setParentType("List");
		listField.add(smpField2);
		
		
		SmpField smpField3 = new SmpField();
		smpField3.setFieldCode("summary");
		smpField3.setInOut("01");
		smpField3.setFieldType("01");
		smpField3.setStruct("01");
		smpField3.setFieldRequire("01");
		smpField3.setNodeType("01");
		smpField3.setParentType("Detail");
		listField.add(smpField3);
		
		
		
		SmpField smpField4 = new SmpField();
		smpField4.setFieldCode("resMsg");
		smpField4.setInOut("02");
		smpField4.setFieldType("01");
		smpField4.setStruct("01");
		smpField4.setFieldRequire("01");
		smpField4.setNodeType("01");
		smpField4.setParentType("");
		listField.add(smpField4);
		
		
		XsdUtil.createSchema(listField,"R000000","FMS");
	}
	
	
	public static  Document createSchema(List<SmpField> listField,String regCode,String sysShort) throws IOException{
		
	
		
		
		Document doc = DocumentHelper.createDocument();
		
		Namespace namespace = new Namespace("", "http://www.w3.org/2001/XMLSchema");
		QName qName = new QName("schema",namespace);
		Element schema = doc.addElement(qName);
		
		schema.addAttribute("targetNamespace", "http://www.ahrcu.com/service/bd/"+regCode);
		schema.addNamespace("bd1", "http://www.ahrcu.com/service/bd/"+regCode);
		schema.addNamespace("hd", "http://www.ahrcu.com/service/hd/"+sysShort.toLowerCase());
		schema.addNamespace("in", "http://www.ahrcu.com/common/header/in");
		
		Element importElement =  schema.addElement("import");
		importElement.addAttribute("schemaLocation", "../Common/"+sysShort+"/SRV"+sysShort+"Head.xsd");
		importElement.addAttribute("namespace", "http://www.ahrcu.com/service/hd/"+sysShort.toLowerCase());

		//request
		
		 //Element requestElement =  schema.addElement("request");
		 
		 
		// requestElement.addAttribute("type", "bd1:SrvReqBody");
		 createElement(schema, "request", "bd1:SrvReqBody");
		 Element  requestcomplexType =  schema.addElement("complexType");
		 requestcomplexType.addAttribute("name", "SrvReqBody");
		 Element requestSequence = requestcomplexType.addElement("sequence");
		 Element bizHeader =  createElement(requestSequence, "bizHeader", "hd:SRVReqHead");
		 bizHeader.addElement("annotation");
		 Element bizBody =  createElement(requestSequence, "bizBody", "bd1:SrvReqBizBody");
		 bizBody.addElement("annotation");
		 createComplexType(schema,"SrvReqBizBody",listField, "02", "01","");

		 
			/*
			 * Element responseElement = schema.addElement("response");
			 * responseElement.addAttribute("type", "bd1:SrvResBody");
			 * 
			 */
		 createElement(schema, "response", "bd1:SrvResBody");
		 Element  responsecomplexType =  schema.addElement("complexType");
		 responsecomplexType.addAttribute("name", "SrvResBody");
		 Element responseSequence = responsecomplexType.addElement("sequence");
		 Element resbizHeader =  createElement(responseSequence, "bizHeader", "hd:SRVResHead");
		 resbizHeader.addElement("annotation");
		 Element resbizBody =  createElement(responseSequence, "bizBody", "bd1:SrvResBizBody");
		 resbizBody.addElement("annotation");
		 createComplexType(schema,"SrvResBizBody",listField, "02", "02","");
		
		
			/*
			 * OutputFormat format = OutputFormat.createPrettyPrint(); // 设置输出编码
			 * format.setEncoding("UTF-8"); // 去除空行 format.setSuppressDeclaration(true);
			 * //format.setNewlines(false); //format.setNewLineAfterDeclaration(false); //
			 * 输出文件 // File file = new File(tarPath +File.separator+ system + "_" +
			 * pubSrvCode + // ".xml"); XMLWriter writer;
			 * 
			 * writer = new XMLWriter(System.out, format); writer.setEscapeText(false);
			 * writer.write(doc);
			 */
		return doc;
	}
	
	
	
	public static Element createElement(Element parentElement,  String name,String type) {
		Element   element = parentElement.addElement("element");
		element.addAttribute("name", name);
		element.addAttribute("type", type);
		return element;
	}
	
	
	public static Element createElement(Element parentElement,  String name,String type,String minOccurs) {
		Element   element = parentElement.addElement("element");
		element.addAttribute("name", name);
		element.addAttribute("type", type);
		element.addAttribute("minOccurs", minOccurs);
		return element;
	}
	
	public static Element createElement(Element parentElement,  String name,String type,String minOccurs,String maxOccurs) {
		Element   element = parentElement.addElement("element");
		element.addAttribute("name", name);
		element.addAttribute("type", type);
		element.addAttribute("minOccurs", minOccurs);
		element.addAttribute("maxOccurs", maxOccurs);
		return element;
	}
	
	
	public static void createComplexType(Element schema,String complexName , List<SmpField> listField, String struct, String inOut,String parent ) {
		Element complexElement =   schema.addElement("complexType");
		complexElement.addAttribute("name", complexName);
		Element sequence = complexElement.addElement("sequence");
		for(SmpField smpField:listField) {
			if(smpField.getParentType()==null) {
				smpField.setParentType("");
			}
			if(struct.equals(smpField.getStruct())&& inOut.equals(smpField.getInOut()) && parent.equals(smpField.getParentType())) {
				if("01".equals(smpField.getNodeType())) {//单节点
					createElement(sequence, smpField.getFieldCode(),"01".equals(smpField.getFieldType())?"string":"number", smpField.getFieldRequire()=="01"?"1":"0");
					
				}else if("03".equals(smpField.getNodeType())) {//List节点
					
					createElement(sequence, smpField.getFieldCode(),"01".equals(smpField.getInOut())?"bd1:req"+smpField.getFieldCode()+"NodeType":"bd1:res"+smpField.getFieldCode()+"NodeType", "0","1");
					createComplexType(schema, "01".equals(smpField.getInOut())?"req"+smpField.getFieldCode()+"NodeType":"res"+smpField.getFieldCode()+"NodeType", listField, struct, inOut, smpField.getFieldCode());
					/*
					 * Element element = e1.addElement(smpField.getFieldCode());
					 * createElement(element, listField, struct, inOut,smpField.getFieldCode());
					 */
				}else if("02".equals(smpField.getNodeType())) {//detail节点
					/*
					 * Element element = e1.addElement(smpField.getFieldCode());
					 * element.addAttribute("type", "group"); createElement(element, listField,
					 * struct, inOut,smpField.getFieldCode());
					 */
					createElement(sequence, smpField.getFieldCode(),"01".equals(smpField.getInOut())?"bd1:req"+smpField.getFieldCode()+"NodeType":"bd1:res"+smpField.getFieldCode()+"NodeType", "0","unbounded");
					createComplexType(schema, "01".equals(smpField.getInOut())?"req"+smpField.getFieldCode()+"NodeType":"res"+smpField.getFieldCode()+"NodeType", listField, struct, inOut, smpField.getFieldCode());

				}
			}
			
		}
		
		
		
	}
	

}
